//
//  RegularRestrictions.m
//  51life
//
//  Created by yixiaoshan on 16/1/30.
//  Copyright © 2016年 HYcompany. All rights reserved.
//

#import "RegularRestrictions.h"

@implementation RegularRestrictions

/**
 * 判断是否为邮箱格式
 * @param strEmail
 * @return
 */
+ (BOOL)IsEmail:(NSString *)strEmail
{
    NSString *regex = @"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strEmail];
//    if (!isMatch)
//    {
//        [SVProgressHUD showErrorWithStatus:@"请输入正确邮箱"];
//    }
    return isMatch;
}

/**
 * 判断是否为手机号码
 * @param strPhoneNumber
 * @return
 */
+ (BOOL)IsPhoneNumber:(NSString *)strPhoneNumber
{
    NSString *regex = @"^1[3|4|5|7|8][0-9]\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strPhoneNumber];
    return isMatch;
}

/**
 * 判断是否为固定电话
 * @param strLandlinePhone
 * @return
 */
+ (BOOL)IsLandlinePhone:(NSString *)strLandlinePhone
{
    NSString *regex = @"^(\\d{3,4}-)\\d{7,8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strLandlinePhone];
    return isMatch;
}

/**
 * 判断是否为身份证
 * @param strIDCard
 * @return
 */
+ (BOOL)IsIDCard:(NSString *)strIDCard
{
    NSString *regex = @"^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X|x)$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strIDCard];
    return isMatch;
}

/**
 * 判断是否为价格（允许0开头小数点后最多两位）(08.80,8.都能通过)
 * @param strPrice
 * @return
 */
+ (BOOL)IsPrice:(NSString *)strPrice
{
    NSString *regex = @"^\\d*[1-9]\\d*\\.\\d{0,2}|\\d*\\.\\d[1-9]|\\d*\\.[1-9]\\d{0,1}|\\d*[1-9]\\d*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strPrice];
    if (!isMatch)
    {
        NSString *regex = @"^\\d*\\.\\d{3,}";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        BOOL isMatch = [pred evaluateWithObject:strPrice];
        if (isMatch)
        {
            [SVProgressHUD showErrorWithStatus:@"金额最多有两位小数"];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:@"请输入正确金额"];

        }
    }
    return isMatch;
}

/**
 * 判断是否为价格（不允许0开头小数点后最多两位）(08.80,8.不能通过)
 * @param strPrice
 * @return
 */
+ (BOOL)IsPriceNotBeginningZero:(NSString *)strPrice
{
    NSString *regex = @"^[1-9]\\d*\\.\\d{1,2}|[0]\\.\\d[1-9]|[0]\\.[1-9]\\d{0,1}|[1-9]\\d*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strPrice];
    if (!isMatch)
    {
        NSString *regex = @"^\\d*\\.\\d{3,}";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        BOOL isMatch = [pred evaluateWithObject:strPrice];
        if (isMatch)
        {
            [SVProgressHUD showErrorWithStatus:@"金额最多有两位小数"];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:@"请输入正确金额"];
            
        }
    }
    return isMatch;
}

/**
 * 判断是否为正整数
 * @param strWholeNumber
 * @return
 */
+ (BOOL)IsWholeNumber:(NSString *)strWholeNumber
{
    NSString *regex = @"^\\d*[1-9]\\d*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:strWholeNumber];;
}

/**
 * 判断是否为数字
 * @param strNum
 * @return
 */
+ (BOOL)IsNum:(NSString *)strNum
{
    NSString *regex = @"^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strNum];
    if (!isMatch)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确数字"];
    }
    return isMatch;
}

/**
 * 判断是否为浮点型数据
 * @param strFloat
 * @return
 */
+ (BOOL)IsFloat:(NSString *)strFloat
{
    NSString *regex = @"^(|[+-]?(0|([1-9]\\d*)|((0|([1-9]\\d*))?\\.\\d{1,2})){1,1})$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:strFloat];
    if (!isMatch)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确浮点数"];
    }
    return isMatch;
}


+ (BOOL)IsCorrentPWDformatter:(NSString *)password {
    NSString *regex = @"^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
}
@end
